package com.ycfmedical.bed.ui;

import com.bilibili.magicasakura.widgets.TintButton;
import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.R;
import com.ycfmedical.bed.base.BaseMenuActivity;
import com.ycfmedical.bed.utils.ResourceUtil;

import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.log.XLog;
import cn.droidlover.xdroidmvp.router.Router;

/**
 * Created by Administrator on 2017-03-23.
 */

public class BodyWeightActivity extends BaseMenuActivity {

    @BindView(R.id.weight_scale_one)
    TintButton weightScaleOne;
    @BindView(R.id.weight_scale_two)
    TintButton weightScaleTwo;
    BluetoothAdapter mBluetoothAdapter;
    //权限判断返回值
    final int MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION = 1;

    @Override
    public void initData(Bundle savedInstanceState) {
        //初始化蓝牙权限问题
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // SDK 版本大于23 android 6.0时进行处理
            initBluetoothPermissions();
        } else {
            // SDK 版本小于23，不管
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_bodyweight;
    }

    @Override
    public Object newP() {
        return null;
    }

    public static void launch(Activity activity) {
        Router.newIntent(activity).to(BodyWeightActivity.class).launch();
    }

    @OnClick({R.id.weight_scale_one, R.id.weight_scale_two})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.weight_scale_one:
                judgeBluetoothState();
                Router.newIntent(this).requestCode(Constants.RESULT_WEIGHT).to(BodyWeightShowActivity.class).launch();
                break;
            case R.id.weight_scale_two:
                judgeBluetoothState();
                Router.newIntent(this).requestCode(Constants.RESULT_WEIGHT).to(BodyWeightShowActivity.class).launch();
                break;
        }
    }

    /**
     * 判断设备蓝牙是否存在
     * 判断蓝牙开启状态（未开启则自动开启）
     */

    private void judgeBluetoothState() {
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            Toast.makeText(this, R.string.bluetooth_remind, Toast.LENGTH_LONG).show();
            finish();
            return;
        }
        if (!mBluetoothAdapter.isEnabled()) {
            //自动开蓝牙
            mBluetoothAdapter.enable();
        }
    }

    /**
     * 6.0以上蓝牙权限获取
     */
    private void initBluetoothPermissions() {

        //判断是否有权限
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            //请求权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                    MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION);
            //判断是否需要 向用户解释，为什么要申请该权限
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_CONTACTS)) {
                Toast.makeText(this, R.string.bluetooth_remind, Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * 权限处理返回
     * 6.0以上蓝牙权限获取
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_ACCESS_COARSE_LOCATION:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    //6.0权限获取成功

                } else {
                    //6.0权限获取失败
                    getvDelegate().toastShort(ResourceUtil.getResources().getString(R.string.bluetooth_permission_failure));
                    finish();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode){
            case Constants.RESULT_WEIGHT:
                judgeBluetoothState();
                Router.newIntent(this).requestCode(Constants.RESULT_WEIGHT).to(BodyWeightShowActivity.class).launch();
                break;
        }
    }

}
